import { post, fetch } from 'plugins/axios'
import { Dialog, QSpinnerGears } from 'quasar'

export function serviceEnv ({state, commit}) {
  state.positionModal = true
  fetch('/service/get_host/', {
    env: state.fields.env
  }).then((response) => {
    commit('envSuccess', response)
  }).catch((error) => {
    console.log(error.response)
  })
}

export function save ({state, commit}) {
  Dialog.create({
    title: '新增服务',
    message: '确定新增服务？',
    ok: '提交',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    post('/service/',
      state.fields
    ).then((response) => {
      commit('saveSuccess')
    }).catch((error) => {
      if (error.response.data) {
        commit('saveFail', error.response.data)
      }
    })
  }).catch(() => {
    console.log('stay')
  })
}
